/*
 *  send format :{requestError:}
 * 	input = {'cusphone': '15824511223','cuspassword':'123456'};
 */
function logIn(req, res, next) {
	console.log("login");
	console.log(req.body.name + ': asdasfasdfas' + req.body.password);
	console.log('body is :' + req.body);

	//success
	var result = {'resutlCode': 0, 'cusphone':req.body.cusphone};
	res.setHeader("Set-Cookie", ["name='admin'"]);
	res.send(result);
	return next();
}

function logOff(req, res, next) {
	console.log("logoff");
	console.log(req.body.latitude + req.body.longitude);
	res.send({
		result : 'This is post ' + req.body.latitude +'   ' + req.body.longitude
	});
	return next();
}

function logOffGet(req, res, next) {
	console.log("logoffGet");
	console.log(req.query.latitude + req.query.longitude);
	res.send({
		result : "this is the latitude:" + req.query.latitude +' ' + req.query.longitude
	});
	return next();
}
function logOffTest(req, res, next) {
	console.log("logoff");
	var mongodb = require('mongodb');
	var dbserver = mongodb.Server('localhost', 27017, {
		auto_reconnect : true
	});
	var db = mongodb.Db('test', dbserver, {
		safe : true
	});
	db.open(function(err, db) {
		if (!err) {
			console.log('connect');
			var collection = db.collection('test');
			console.log('after collection');
			collection.insert({
				'result' : 'connect2'
			}, {
				w : 1
			}, function(err, result) {
				console.log('begin');
				collection.count(function(err, count) {
					console.log(format("count = %s", count));
				});
				if (!err) {
					console.log(result);
				} else {
					console.log(err);
				}
			});
			res.send({
				result : 'connnect'
			});
			db.close();
		} else {
			console.log(err);
		}

	});

	return next();
}

function testdb(req, res, next) {
	var Mongodb = require('../DAOImpl/mongodb');
	//var db = Mongodb.dbClient("mongodb://localhost:27017/test", 'test');
	//var db =Mongodb.dbClient();
    //db.insert();
	var user = require('../DAOImpl/user').User;
	user.show1();
	//user.show2();

	res.send({
		result : 'connnect'
	});
	return next();
}
exports.services = [ logIn, logOff, logOffTest, testdb, logOffGet ];
